/* 
  Copyright Software Engineering Research laboratory <serl@cs.wichita.edu>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 License as published by the Free Software Foundation; either
 version 2 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Library General Public License for more details.

 You should have received a copy of the GNU Library General Public
 License along with this program; if not, write to the Free
 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
package wichita.edu.codereview;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.json.JSONException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/*
 * 
 *  @author: Sara Bahrami <mxbahramizanjani@wichita.edu>
 */

/*
 * @purpose: Main class of this package- 
 * @param1 : urlpath of Gerrit code review system of project(eg., Mylyn code review)
 * @param2: sortkey of last review in Gerrit code review system for start  
 * calls WritingJsontoXML
 */
public class Run {
	public static String dirPath1 = "/home/sunshine40270/JAVA/Eclipse_workspace/";
	public static void main(String args[]) throws JSONException, ParserConfigurationException, TransformerException
	{
		//String urlpath="https://git.eclipse.org/r/changes/?q=mylyn&o=ALL_REVISIONS&o=ALL_FILES&o=MESSAGES&n=1";
		String urlpath="https://git.eclipse.org/r/changes/?q=mylyn&o=ALL_REVISIONS&o=ALL_FILES&o=MESSAGES";
		//making docnew for writing to new XML file
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		Document docnew = docBuilder.newDocument();
		//Adding rootelement "Reviews" to docnew
		Element rootElement = docnew.createElement("Reviews");
		docnew.appendChild(rootElement);
		//Sending the first review from Gerrit to write in XML file
		String sortkey="002ff6de000020cf";
		String url=urlpath+"&n=1&N="+sortkey;
		Boolean morechange=returnhasMoreChange(urlpath,sortkey);

		while(morechange==true)
			
		{
			
			
			if (reviewElement(docnew, urlpath, sortkey)!=null) 
			
				rootElement.appendChild(reviewElement(docnew, urlpath, sortkey));
				sortkey = returnSortKey(urlpath, sortkey);
				System.out.println(sortkey);
				
				morechange = returnhasMoreChange(urlpath, sortkey);
				//System.out.println(morechange);

		}
		
		documenttoFile(docnew);
		
	}
	public static String returnSortKey(String urlpath,String sortkey) throws JSONException
	{	//String url="https://git.eclipse.org/r/changes/?q=mylyn&o=ALL_REVISIONS&o=ALL_FILES&o=MESSAGES&n=2&N="+sortkey+"\"";
		String url=urlpath+"&n=2&N="+sortkey;
		JsonArrayParser obj=new JsonArrayParser(url);
		String newsortkey=obj.getSortkey();
		return newsortkey;
		
	}
	
	public static Boolean returnhasMoreChange(String urlpath,String sortkey) throws JSONException
	{	//String url="https://git.eclipse.org/r/changes/?q=mylyn&o=ALL_REVISIONS&o=ALL_FILES&o=MESSAGES&n=2&N="+sortkey+"\"";
		String url=urlpath+"&n=1&N="+sortkey;
		JsonArrayParser obj=new JsonArrayParser(url);
		Boolean morechange=obj.hasMoreChange();
		return morechange;
		
	}
	
	//for each review in Gerrit returns related review Element 
	public static Element reviewElement(Document docnew,String urlpath,String sortkey) throws JSONException, ParserConfigurationException, TransformerException
	{
		//String url="https://git.eclipse.org/r/changes/?q=mylyn&o=ALL_REVISIONS&o=ALL_FILES&o=MESSAGES&n=1&N="+sortkey+"\"";
		String url=urlpath+"&n=1&N="+sortkey;
		WritingJsontoXML obj=new WritingJsontoXML(url,docnew);
		return(obj.reviewJSONtoXML());
	}
	
	//Writing docnew to result.xml file
	public static void documenttoFile(Document docnew) throws TransformerException
	{
		//Writing docnew to result.xml file
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT,"Yes");
		StreamResult result = new StreamResult(new File(dirPath1+"resultallreviews.xml"));
		DOMSource source = new DOMSource(docnew);
		transformer.transform(source, result);
	}

}
